﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Microsoft.Services.TfsAlert.AppCode
{
	public static class Strobe
	{
        private static bool _activateLight = false;
        public static bool IsLightOn = false;
        private static object _syncRoot = new object();

        public static void ActivateLight(bool activateLight)
        {
            lock ( _syncRoot )
            {
                _activateLight = activateLight;
            }
        }

        public static void TurnLightOn()
        {
            RelayIO relay = null;

            lock ( _syncRoot )
            {
                if ( _activateLight )
                {
                    try
                    {
                        relay = new RelayIO();

                        relay.Open();
                        relay.TurnRelayOn( 3 );
                        relay.Close();

                        IsLightOn = true;
                    }
                    catch ( Exception ex )
                    {
                        StringBuilder errorMessage = new StringBuilder();
                        errorMessage.AppendFormat( "TfsAlert Errored in turnLightOn: \n{0}", ex.Message );
                        Trace.TraceInformation( ex.Message );
                    }
                }
            }
        }

        public static void TurnLightOff()
        {
            RelayIO relay = null;

            lock ( _syncRoot )
            {
                try
                {
                    relay = new RelayIO();
                    relay.Open();
                    relay.TurnRelayOff( 3 );
                    relay.Close();

                    IsLightOn = false;

                }
                catch ( Exception ex )
                {

                    StringBuilder errorMessage = new StringBuilder();
                    errorMessage.AppendFormat( "TfsAlert Errored in turnLightOff: \n{0}", ex.Message );
                    Trace.TraceInformation( ex.Message );

                }
            }
        }
	}
}
